package chapt03.pstipple;
// PStipple.java
// OpenGL SuperBible
// Demonstrates OpenGL Polygon Stippling
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;

public class PStipple
{
	// Define a constant for the value of PI
	static float GL_PI = 3.1415f;
	
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// Bitmap of camp fire
	static byte fire[] = {
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x00, (byte)0xc0,
		(byte)0x00, (byte)0x00, (byte)0x01, (byte)0xf0,
		(byte)0x00, (byte)0x00, (byte)0x07, (byte)0xf0,
		(byte)0x0f, (byte)0x00, (byte)0x1f, (byte)0xe0,
		(byte)0x1f, (byte)0x80, (byte)0x1f, (byte)0xc0,
		(byte)0x0f, (byte)0xc0, (byte)0x3f, (byte)0x80,	
		(byte)0x07, (byte)0xe0, (byte)0x7e, (byte)0x00,
		(byte)0x03, (byte)0xf0, (byte)0xff, (byte)0x80,
		(byte)0x03, (byte)0xf5, (byte)0xff, (byte)0xe0,
		(byte)0x07, (byte)0xfd, (byte)0xff, (byte)0xf8,
		(byte)0x1f, (byte)0xfc, (byte)0xff, (byte)0xe8,
		(byte)0xff, (byte)0xe3, (byte)0xbf, (byte)0x70, 
		(byte)0xde, (byte)0x80, (byte)0xb7, (byte)0x00,
		(byte)0x71, (byte)0x10, (byte)0x4a, (byte)0x80,
		(byte)0x03, (byte)0x10, (byte)0x4e, (byte)0x40,
		(byte)0x02, (byte)0x88, (byte)0x8c, (byte)0x20,
		(byte)0x05, (byte)0x05, (byte)0x04, (byte)0x40,
		(byte)0x02, (byte)0x82, (byte)0x14, (byte)0x40,
		(byte)0x02, (byte)0x40, (byte)0x10, (byte)0x80, 
		(byte)0x02, (byte)0x64, (byte)0x1a, (byte)0x80,
		(byte)0x00, (byte)0x92, (byte)0x29, (byte)0x00,
		(byte)0x00, (byte)0xb0, (byte)0x48, (byte)0x00,
		(byte)0x00, (byte)0xc8, (byte)0x90, (byte)0x00,
		(byte)0x00, (byte)0x85, (byte)0x10, (byte)0x00,
		(byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00,
		(byte)0x00, (byte)0x00, (byte)0x10, (byte)0x00
	};		
		
	
	// Setup the rendering state
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Black background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		// Set drawing color to red
		gl2.glColor3f(1.0f, 0.0f, 0.0f);
		
		// Enable polygon stippling
		gl2.glEnable(GL2.GL_POLYGON_STIPPLE);
		
		// Specify a specific stipple pattern
		gl2.glPolygonStipple(fire, 0);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float nRange = 100.0f;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset projection matrix stack
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		if(w <= h)
			gl2.glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
		else
			gl2.glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
		
		// Reset projection matrix stack
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Clear the window with the current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Save matrix state and do the rotation
		gl2.glPushMatrix();
		gl2.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
		gl2.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
		
		// Begin the stop sign shape,
		// use a standard polygon for simplicity
		gl2.glBegin(GL2.GL_POLYGON);
			gl2.glVertex2f(-20.0f, 50.0f);
			gl2.glVertex2f(20.0f, 50.0f);
			gl2.glVertex2f(50.0f, 20.0f);
			gl2.glVertex2f(50.0f, -20.0f);
			gl2.glVertex2f(20.0f, -50.0f);
			gl2.glVertex2f(-20.0f, -50.0f);
			gl2.glVertex2f(-50.0f, -20.0f);
			gl2.glVertex2f(-50.0f, 20.0f);
		gl2.glEnd();
		
		// Restore transformations
		gl2.glPopMatrix();
		
		// Flush drawing commands
		gl2.glFlush();
		
	}
}
